using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShoppingWebPage.Models;
using ShoppingWebPage.ViewModels;

namespace ShoppingWebPage.Controllers
{   
    public class CartsController : Controller
    {
        private ShoppingWebPageContext context = new ShoppingWebPageContext();

        //
        // GET: /Carts/

        public ViewResult Index()
        {
            return View(context.Carts.Include(cart => cart.Items).ToList());
        }

        //
        // GET: /Carts/Details/5

        public ViewResult Details(int id)
        {
            Cart cart = context.Carts.Single(x => x.id == id);
            return View(cart);
        }

        //
        // GET: /Carts/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Carts/Create

        [HttpPost]
        public ActionResult Create(Cart cart)
        {
            if (ModelState.IsValid)
            {
                context.Carts.Add(cart);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(cart);
        }
        
        //
        // GET: /Carts/Edit/5
 
        public ActionResult Edit(string id)
        {
            var temp = new Cart();
            var cart = temp.NewCart(id);
            var cartViewModel = new CartViewModel();
            cartViewModel.cart = cart;
            cartViewModel.totalBalance = cart.Items.Sum(i => i.itemTotalPrice);
            cartViewModel.totalItems = cart.Items.Sum(i => i.quantity);
            return View(cartViewModel);
        }

        //
        // POST: /Carts/Edit/5

        [HttpPost]
        public ActionResult Edit(CartViewModel cartViewModel)
        {
            var temp = new Cart();
            var cart = temp.NewCart(cartViewModel.cart.cartName);
            cart.Items.Clear();
            foreach (var item in cartViewModel.cart.Items)
            {
                temp.UpdateItems(cart, item);
            }
            temp.SaveCart(cart);
            return RedirectToAction("Edit", "Carts");
        }

        //
        // GET: /Carts/Delete/5
 
        public ActionResult Delete(int id)
        {
            Cart cart = context.Carts.Single(x => x.id == id);
            return View(cart);
        }

        //
        // POST: /Carts/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Cart cart = context.Carts.Single(x => x.id == id);
            context.Carts.Remove(cart);
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                context.Dispose();
            }
            base.Dispose(disposing);
        }

        public RedirectToRouteResult AddToCart(int id)
        {
            var item = context.Items.First(i => i.id == id);
            var cart = new Cart();
            item.quantity++;
            cart.AddToCart(item);
            return RedirectToAction("Edit", new { id = "Cart" });
        }
    }
}